package com.zl.easybuy.web;

import com.zl.easybuy.pojo.News;
import com.zl.easybuy.pojo.Order;
import com.zl.easybuy.pojo.OrderDetail;
import com.zl.easybuy.service.NewsService;
import com.zl.easybuy.service.OrderService;
import com.zl.easybuy.util.Pages;
import com.zl.easybuy.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    NewsService newsService;
    @Autowired
    OrderService orderService;

    @GetMapping("/news/queryNewsList")
    public String queryNewsList(@RequestParam(defaultValue = "1") Integer pageNo, Model model){
        Pages<News> newsBoPages=new Pages<>();
        newsBoPages=newsService.findNewsBy(pageNo);
        model.addAttribute("pager",newsBoPages);
        return "forward:/backend/news/newsList.jsp";
    }
    @GetMapping("/news/newsDeatil")
    public String queryNewsDeatil(Integer id, Model model){
        News news=new News();
        news=newsService.findNewById(id);
        model.addAttribute("news",news);
        return "forward:/backend/news/newsDetail.jsp";
    }
    @GetMapping("/user/index")
    public String index(){
        if(SessionUtil.getCurrentUser()==null){
            return "redirect:/pre/login.jsp";
        }
        return "/backend/user/userInfo";
    }
    @GetMapping("/order/index")
    public String order(Integer userId,Model model,@RequestParam(defaultValue = "1") Integer pageNo){
        if(userId==null){
            return "redirect:/pre/login.jsp";
        }
        Pages<Order> orderList=new Pages<>();
        orderList=orderService.findOrderById(userId,pageNo);
        model.addAttribute("pager",orderList);
        return "/backend/order/orderList";
    }
    @RequestMapping("/order/queryAllOrder")
    public String queryAllOrder(Model model,@RequestParam(defaultValue = "1") Integer pageNo){
        Pages<Order> orderList=new Pages<>();
        orderList=orderService.findAllOrderBy(pageNo);
        model.addAttribute("pager",orderList);
        return "/backend/order/orderList";
    }

}
